#ifndef IMGDIRLOADER_H
#define IMGDIRLOADER_H

#include <QtCore/QObject>
#include <QtGui/QImage>
#include <QtGui/QImageReader>
#include <QtCore/QString>
#include <QtCore/QMap>
#include <QtCore/QStringList>
#include <QtCore/QFuture>
#include <QtCore/QMutex>
#include <QtCore/QDateTime>

class ImgDirLoader : public QObject
{
	Q_OBJECT

public:
	ImgDirLoader(const QString& dirPath,QObject *parent=0);
	virtual ~ImgDirLoader();

	void startLoad(const QStringList&);
	void stopLoading();

	const QImage& image(const QString& filePath) const;

	QDateTime lastAccessTime() const;
	void clearLoadedImages();
	QString dirPath() const{
		return _dirPath;
	}
signals:
	void imageLoaded(const QString& filePath);
private:
	const QString& _dirPath;
	typedef QMap<QString/*file*/,QImage> ImageMap;
	mutable ImageMap _imageMap;
	mutable QMutex  _imgMapMutex;

	QStringList  _residualImgFiles;
	QStringList getResidualImgFiles(const QStringList& imgFiles) const;

	void loadImageFromFile(const QString&  file);

	QFuture<void> _loadingFuture;
	bool _loadingFinished;

	QDateTime _lastAccessTm;
};

#endif // IMGDIRLOADER_H
